<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/3
 * Time: 14:51
 */
namespace app\mtadminapi\controller;

use app\util\ReturnCode;
use think\cache\driver\Redis;
use app\mtadminapi\model\MtAdminSysAccount; // 管理员
use think\Controller;
use think\Request;
use think\facade\Hook;
use think\facade\Session;

class AdminLogin extends Base
{
    public function __construct(Request $request){
        parent::__construct($request);
    }

    /**
     * 后台管理系统登录
     * @param string $userName 用户名
     * @param string $passWord 密码
     */
    public function adminLogin(){
        $userName = $this->request->post("userName");
        $passWord = $this->request->post("passWord");
        if(!$userName || !$passWord){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"参数缺少,请检查用户名或密码");
        }
        $obj = new MtAdminSysAccount();
        $userPassWordArr = $obj->where([["userName","=",$userName],["status","=","1"]])->field("adminID,userName,passWord,shopID,sysHeaderImg")->find();
        if($userPassWordArr){
            $userPassWordArr->toArray();
            if(password_verify($passWord, $userPassWordArr['passWord'])){
                $adminAuth = $this->adminAuth($userPassWordArr['adminID']); // 此处获取权限
                if(!$adminAuth){
                    return $this->bindError(ReturnCode::AUTH_ERROR,"权限读取错误");
                }
                $this->adminUser['accountAuth'] = $adminAuth;
                $this->adminUser['adminInfo'] = [
                    'shopID' => $userPassWordArr['shopID'],
                    'sysHeaderImg' => $userPassWordArr['sysHeaderImg']
                ];
                $this->adminID = $userPassWordArr['adminID'];
                Hook::listen("AdminApiLoginLog",['adminID' => $userPassWordArr['adminID']]);
                $token = $this->getToken();
                if($token['code'] != 1){
                    return $this->bindError(ReturnCode::AUTH_ERROR,"token生成错误");
                }
                $this->adminUser['token'] = $token['token'];
//                //存入session
                \session("adminUser.adminID",$token['token']);// adminID
//                //存入redis
                (new Redis)->set("userToken".$userPassWordArr['adminID'],$this->adminUser['token'],600); // token存储
                (new Redis)->set($token['token'],$userPassWordArr['adminID'],600); // 登录状态
                (new Redis)->set("adminAuth.".$userPassWordArr['adminID'],json_encode($adminAuth,JSON_UNESCAPED_UNICODE),600); // 登录权限
                Hook::listen("AdminApiLoginLog",['adminID' => $userPassWordArr['adminID']]); // 管理员登录日志钩子
                $this->adminUser['adminID'] = $this->adminID;
                return $this->bindSuccess("登录成功",$this->adminUser);
            }else{
                return $this->bindError(ReturnCode::RECORD_NOT_FOUND,"密码错误");
            }
        }else{
            return $this->bindError(ReturnCode::RECORD_NOT_FOUND,"用户名错误");
        }
    }

    /**
     * 管理员权限
     * @return array|bool
     */
    public function adminIdAuth(){
        $adminID = $this->request->param("adminID");
        $auth = $this->adminAuth($adminID);
        return $auth;
    }

    /**
     * 后台管理员退出登录
     * @return array
     */
    public function adminExit(){
        $adminID = $this->request->param("adminID");
        if(!$adminID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"adminID不存在");
        }
        \session("adminUser.adminID",null);
        $redis = new Redis();
        $redis->rm("userToken".$adminID);
        $redis->rm($this->adminUser['token']);
        $redis->rm("adminAuth.".$adminID);
        if(!$redis->has("userToken") && !$redis->has($this->adminUser['token']) && !$redis->has("adminAuth.".$adminID)){
            return $this->bindSuccess("退出成功",$this->adminUser);
        }
    }
}